package AgentsDB;

import java.io.*;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class AgentDataIO {
	
	static public void writeAgents(Set<Agent> agents, String filePath, String separator) throws IOException{

		Set<Agent> existedAgents = readAgents(filePath, separator);
		if(existedAgents.size()!=0){
			Iterator<Agent> iterator = agents.iterator();
			while(iterator.hasNext()){
				existedAgents.add(iterator.next());
			}
			File file = new File(filePath);
			FileWriter fileWriter = new FileWriter(file);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
			iterator = existedAgents.iterator();
			while(iterator.hasNext()){
				Agent agent = iterator.next();
				bufferedWriter.write(agent.getPhoneNumber()+separator+agent.getAgentName()+separator+agent.getDate()+separator+agent.getUrl());
				bufferedWriter.newLine();
			}
			fileWriter.flush();
			bufferedWriter.flush();
			fileWriter.close();
			bufferedWriter.close();
		}else{
			System.out.println("The agent data file is empty");
		}
	}
	static public void writeAgentSetObject(Set<Agent> agents, String filePath)throws IOException{
		FileOutputStream fileOutputStream = new FileOutputStream(filePath);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		objectOutputStream.writeObject(agents);
		objectOutputStream.flush();
		fileOutputStream.flush();
		objectOutputStream.close();
		fileOutputStream.close();
	}
	static public Set<Agent> readAgentSetObject(String filePath)throws IOException, ClassNotFoundException{
		FileInputStream fileInputStream = new FileInputStream(filePath);
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		Set<Agent> agents = (Set<Agent>) objectInputStream.readObject();
		objectInputStream.close();
		fileInputStream.close();
		return agents;
	}
	static public Agent readAgent(String agentLine, String separator){
         String[] agentString = new String[5];
         try {
        	 agentString = agentLine.split(separator);
        	 SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
		
			 Date date = (Date) df.parse(agentString[2]);
			 return new Agent(Long.valueOf(agentString[0]), agentString[1], date, agentString[3]);
		 } catch (Exception e) {
			 System.out.println("ERR_203 Can't parse string line to an agent. String: "+agentLine+" separator: " +separator
						+ e.getMessage());
				e.printStackTrace(System.out);
				// Logger should be applied
		 }
		 
		return null;
	}

	static public Set<Agent> readAgents(String filePath, String separator) throws IOException{
		File file = new File(filePath);
		FileReader fileReader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		Set<Agent> agents = new HashSet<Agent>();
		String readLine;
		Agent agent;

		try {
			while ((readLine = bufferedReader.readLine()) != null) {
				
				agent = readAgent(readLine, separator);
				if(agent!=null)agents.add(agent);

			}
		} catch (Exception e) {
			System.out.println("ERR_202 Can't read line from file."
					+ e.getMessage());
			e.printStackTrace(System.out);
			// Logger should be applied
		}
		fileReader.close();
		bufferedReader.close();
		
		return agents;
	}
	public static void ObjectStoreUpdate(Set<Agent> agents,String objectFilePath) throws IOException, ClassNotFoundException{
		Set<Agent> existedAgents = AgentDataIO.readAgentSetObject(objectFilePath);
		for(Agent a: agents){
			if (a!= null) existedAgents.add(a);
		}
		AgentDataIO.writeAgentSetObject(existedAgents, objectFilePath);
	}
	public static void readStoredAgents(String objectFilePath) {
		Set<Agent> agents;
		try {
			agents = AgentDataIO.readAgentSetObject(objectFilePath);
			for(Agent a: agents){
				System.out.println(a.getPhoneNumber()+" "+a.getAgentName());
			}
			System.out.println(agents.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(System.out);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(System.out);
		}

	}
	public static void writeAgentsToFile(String filePath, String objectFilePath) {
		try{
			File file = new File(filePath);
			FileWriter fileWriter = new FileWriter(file);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				Set<Agent> agents = AgentDataIO.readAgentSetObject(objectFilePath);
				for(Agent a: agents){
					bufferedWriter.write(a.getPhoneNumber()+","+
							a.getAgentName()+","+
							a.getUrl()+","+
							a.getContent()+","+
							a.getDate()+",");
					bufferedWriter.write(System.getProperty("line.separator"));
				}
				
			fileWriter.flush();
			bufferedWriter.flush();
			fileWriter.close();
			bufferedWriter.close();
		}catch(IOException e){
			System.out.println("Couldn't write file");
			e.printStackTrace(System.out);
		} catch (ClassNotFoundException e) {
			System.out.println("Couldn't read Agents");
			e.printStackTrace(System.out);
		}
	}
}
